import { Injectable, Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { ConfigService } from '@nestjs/config';
import { BaseService } from '@wwjCommon/base/base.service';
import { CacheService } from '@wwjCommon/cache/cache.service';
import { LoggingService } from '@wwjCommon/logging/logging.service';
import { UploadService } from '@wwjVendor/upload/upload.service';
import { PayService } from '@wwjVendor/pay/pay.service';
import { SmsService } from '@wwjVendor/sms/sms.service';
import { NoticeService } from '@wwjVendor/notice/notice.service';

@Injectable()
export class CoreSmsService extends BaseService<any> {
  private readonly logger = new Logger(CoreSmsService.name);

  constructor(
    @InjectRepository(Object)
    protected readonly repository: Repository<any>,
    private readonly cacheService: CacheService,
    private readonly configService: ConfigService,
    private readonly loggingService: LoggingService,
    private readonly uploadService: UploadService,
    private readonly payService: PayService,
    private readonly smsService: SmsService,
    private readonly noticeService: NoticeService,
  ) {
    super(repository);
  }

  /**
   * send
   * 对应 PHP: CoreSmsService_core::send()
   * 逻辑类型: undefined - undefined
   */
  async send(site_id: any[], mobile: any[], params: any[], key: any[], template_id: any[], content: any[], url_params: any[]) {
    // 基于PHP真实逻辑: send
    // PHP原文: //查询配置         $config = $this->getDefaultSmsConfig($site_id);         $sms_type = $config['sms_type'];         if(empty($sms_type)) throw new NoticeE...
//查询配置
        const config = this.getDefaultSmsConfig(site_id);
        const sms_type = config.sms_type;
        if(!sms_type)) throw new NoticeBusinessException('SMS_TYPE_NOT_OPEN');
        //创建
        const core_notice_sms_log_service = this.coreNoticeSmsLogService;
        const log_id = core_notice_sms_log_service.add(site_id, [
            mobile: mobile,
            sms_type: sms_type,
            key: key,
            content: content,
            template_id: template_id,
            params: params,
            status: SmsDict.SENDING
        )];
        const sms_driver = new SmsLoader(sms_type, config];
        const params = this.makeUp(params, content, sms_type];
        params.url_params = url_params;
        const result = sms_driver.send(mobile, template_id, params);

        if (!result) {
            //失败修改短信记录
            const error = sms_driver.getError(];
            core_notice_sms_log_service.edit(site_id, log_id, [
                status: SmsDict.FAIL,
                result: sms_driver.getError()
            ]];
            throw new NoticeBusinessException(error);
  }

  /**
   * makeUp
   * 对应 PHP: CoreSmsService_core::makeUp()
   * 逻辑类型: undefined - undefined
   */
  async makeUp(params: any, content: any, sms_type: any) {
    // 基于PHP真实逻辑: makeUp
    // PHP原文: if($sms_type != SmsDict::TENCENTSMS) return $params;         if(empty($params)) return [];         $temp_array = [];         foreach($params as $k => ...
if(sms_type != SmsDict.TENCENTSMS) return params;
        if(!params)) return [];
        const temp_ = [];
        foreach(params as k => v){
            const index = String.prototype.indexOf.call(content, '{' . k  + '}');
            if (index !== false) {
                temp_array[index] = v;
            }
  }

  /**
   * getDefaultSmsConfig
   * 对应 PHP: CoreSmsService_core::getDefaultSmsConfig()
   * 逻辑类型: undefined - undefined
   */
  async getDefaultSmsConfig(site_id: number) {
    // 基于PHP真实逻辑: getDefaultSmsConfig
    // PHP原文: $info = (new CoreConfigService())->getConfig($site_id, ConfigKeyDict::SMS)['value'] ?? [];         if (empty($info))             throw new NoticeExcep...
const info = this.coreConfigService.getConfig(site_id, ConfigKeyDict.SMS).value || [];
        if (!info))
            throw new NoticeBusinessException('NOTICE_SMS_NOT_OPEN'];

        const sms_type = info.default || '';
        const config = [
            sms_type: sms_type,
        ];
        return Object.assign(config, info[sms_type) || [);
  }
  }
}
